package com.skcc.ts.nsi.user;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

import com.skcc.ts.nsi.MEMain;
import com.skcc.ts.nsi.R;
import com.skcc.ts.nsi.service.SocketClientService;

public class MEUsrMain extends Activity {

	private ImageButton btn_usr_prj_regi;
	private ImageButton btn_usr_regi_search;
	private String myId;
	private boolean terminate = false;
	private String addr;
	private String port;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.meusrmain);

		Intent receivedI = getIntent();
		myId = receivedI.getStringExtra("myId");
		addr = receivedI.getStringExtra("loginAddr");
		port = receivedI.getStringExtra("loginPort");

		redoUserMain();
		searchPermission();
		searchTeam();
		checkRegi();// 플젝등록체크
		initButtons();

	}

	private void redoUserMain() {// 프로젝트DB 존재체크
		Intent receiveI = getIntent();
		boolean doYN = receiveI.getBooleanExtra("restart", false);
		if (doYN) {
			myId = receiveI.getStringExtra("myId");
			addr = receiveI.getStringExtra("loginAddr");
			port = receiveI.getStringExtra("loginPort");
			terminate = true;
			new AlertDialog.Builder(MEUsrMain.this).setTitle("알림").setMessage("등록된 프로젝트가 없습니다").setPositiveButton("OK", null).show();
		}
	}

	private void checkRegi() {
		Intent receiveI = getIntent();
		boolean teamRegi = receiveI.getBooleanExtra("teamRegi", false);
		if (teamRegi) {
			myId = receiveI.getStringExtra("myId");
			addr = receiveI.getStringExtra("loginAddr");
			port = receiveI.getStringExtra("loginPort");
			String result = receiveI.getStringExtra("result");
			terminate = true;
			if ("SUCC".equals(result)) {
				new AlertDialog.Builder(MEUsrMain.this).setTitle("등록성공").setMessage("프로젝트 등록 성공하였습니다.").setPositiveButton("OK", null)
						.show();
			} else {
				new AlertDialog.Builder(MEUsrMain.this).setTitle("알림").setMessage("이미 프로젝트에 등록된 상태입니다.").setPositiveButton("OK", null)
						.show();
			}
		}
	}

	private void searchTeam() {
		Intent receiveI = getIntent();
		boolean team = receiveI.getBooleanExtra("noone", false);
		myId = receiveI.getStringExtra("myId");
		addr = receiveI.getStringExtra("loginAddr");
		port = receiveI.getStringExtra("loginPort");
		if (team) {
			new AlertDialog.Builder(MEUsrMain.this).setTitle("알림").setMessage("등록된 팀원이 없습니다.").setPositiveButton("OK", null).show();
		}
	}

	private void searchPermission() {
		Intent receiveI = getIntent();
		boolean permission = receiveI.getBooleanExtra("permission", true);
		myId = receiveI.getStringExtra("myId");
		addr = receiveI.getStringExtra("loginAddr");
		port = receiveI.getStringExtra("loginPort");
		if (!permission) {
			new AlertDialog.Builder(MEUsrMain.this).setTitle("알림").setMessage("조회 권한이 없습니다.").setPositiveButton("OK", null).show();
		}
	}

	private void initButtons() {
		btn_usr_prj_regi = (ImageButton) findViewById(R.id.btn_usr_prj_regi);// 프로젝트 등록
		btn_usr_regi_search = (ImageButton) findViewById(R.id.btn_usr_regi_search);// 팀원등록 및 조회

		btn_usr_regi_search.setOnClickListener(new OnClickListener() {// 팀원조회
					public void onClick(View v) {
						Intent mymanI = new Intent(MEUsrMain.this, SocketClientService.class);
						mymanI.putExtra("id", 12);// 기능분류:사용자관리(팀원조회)
						mymanI.putExtra("myId", myId);
						mymanI.putExtra("loginAddr", addr);
						mymanI.putExtra("loginPort", port);
						mymanI.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
						startService(mymanI);
						if (terminate) {
							finish();
						}
					}
				});

		btn_usr_prj_regi.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent mypjtI = new Intent(MEUsrMain.this, SocketClientService.class);
				mypjtI.putExtra("id", 14);// 기능분류 : 사용자관리(프로젝트등록)
				mypjtI.putExtra("myId", myId);
				mypjtI.putExtra("loginAddr", addr);
				mypjtI.putExtra("loginPort", port);
				mypjtI.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
				startService(mypjtI);
				if (terminate) {
					finish();
				}
			}
		});
	}

	private void showPrjRegi() {
		Intent intent = new Intent(MEUsrMain.this, MEUsrPrjRegist.class);
		// intent.setFlags(intent.FLAG_ACTIVITY_SINGLE_TOP);
		startActivity(intent);
	}

	private void showUserRegiSearch() {
		Intent intent = new Intent(MEUsrMain.this, MEUsrRegiSearch.class);
		startActivity(intent);
	}
}